package cn.jobstart.spider.service.impl;

import cn.jobstart.common.IdGen;
import cn.jobstart.common.NumberUtil;
import cn.jobstart.common.spider.Job51Util;
import cn.jobstart.common.StringUtil;
import cn.jobstart.common.spider.LiePinUtil;
import cn.jobstart.data.DataUtil;
import cn.jobstart.enums.TalentSubscriptionTaskChannelEnum;
import cn.jobstart.es.service.IResumeEsService;
import cn.jobstart.modelExt.SpiderMonitorExt;
import cn.jobstart.modelExt.TalentSubscriptionTaskExt;
import cn.jobstart.pubInter.exception.SkException;
import cn.jobstart.service.IResumeBasicService;
import cn.jobstart.service.ISpiderMonitorService;
import cn.jobstart.service.ITalentSubscriptionTaskService;
import cn.jobstart.spider.SkSpiderMonitor;
import cn.jobstart.spider.TalentJob51Search;
import cn.jobstart.spider.TalentLiePinSearch;
import cn.jobstart.spider.WebDriverPool;
import cn.jobstart.spider.downloader.request.HtmlRequest;
import cn.jobstart.spider.downloader.request.TalentJob51Request;
import cn.jobstart.spider.downloader.request.TalentLiePinRequest;
import cn.jobstart.spider.pageProcessor.HtmlPageProcessor;
import cn.jobstart.spider.pageProcessor.NbrcJobPageProcessor;
import cn.jobstart.spider.pageProcessor.TalentJob51PageProcessor;
import cn.jobstart.spider.pageProcessor.TalentLiePinPageProcessor;
import cn.jobstart.spider.pipeline.HtmlPipline;
import cn.jobstart.spider.pipeline.NbrcJobPipline;
import cn.jobstart.spider.pipeline.TalentJob51Pipline;
import cn.jobstart.spider.pipeline.TalentLiePinPipline;
import cn.jobstart.spider.searchBuild.TalentSearchJob51Build;
import cn.jobstart.spider.searchBuild.TalentSearchLiePinBuild;
import cn.jobstart.spider.service.ISpiderService;
import cn.jobstart.spider.spider.HtmlSpider;
import cn.jobstart.spider.spider.NbrcJobSpider;
import cn.jobstart.spider.spider.TalentJob51Spider;
import cn.jobstart.spider.spider.TalentLiePinSpider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.model.HttpRequestBody;
import us.codecraft.webmagic.utils.HttpConstant;

import javax.annotation.Resource;

/**
 * @author sunke
 * @version 1.0
 * @Description
 * @date 2023-09-19 16:10
 */
@Service
@Slf4j
public class SpiderServiceImpl implements ISpiderService {


    private final String chromeDriverPath = "/Users/sunke/apps/chromedriver-mac-arm64/chromedriver";
    @Resource
    private ITalentSubscriptionTaskService talentSubscriptionTaskService;
    @Resource
    private WebDriverPool webDriverPool;
    @Resource
    private Job51Util job51Util;

    @Resource
    private IResumeBasicService resumeBasicService;
    @Resource
    private IResumeEsService resumeEsService;
    @Resource
    private DataUtil dataUtil;
    @Resource
    private TalentSearchJob51Build job51TalentSearchBuild;
    @Resource
    private ISpiderMonitorService spiderMonitorService;

    /**
     * 直接通过传过的html 解析
     *
     * @param url
     * @param sourceFlag
     * @param html
     * @param adminId
     * @param adminTruename
     * @throws Exception
     */
    @Override
    public void startHtml(String url, int sourceFlag, String html, String adminId, String adminTruename, int typeFlag) throws Exception {
        SpiderMonitorExt spiderMonitor = new SpiderMonitorExt();
        spiderMonitor.setAdminId(adminId);
        spiderMonitor.setAdminTruename(adminTruename);
        spiderMonitor.setId(IdGen.uuid());
        HtmlPageProcessor htmlPageProcessor = HtmlPageProcessor.getProcessor(sourceFlag, typeFlag);
        HtmlSpider spider = new HtmlSpider(htmlPageProcessor, spiderMonitor);
        HtmlRequest request = new HtmlRequest(url);
        request.setHtml(html);
        spider.addRequest(request);

        HtmlPipline htmlPipline = HtmlPipline.getPipline(sourceFlag, typeFlag);
        if (StringUtil.isNotObjEmpty(htmlPipline)) {
            htmlPipline.setResumeEsService(resumeEsService);
        }
        spider.addPipeline(htmlPipline);
        //添加自己的监控
        SkSpiderMonitor.instance().register(spider);
        spider.start();
    }

    /**
     * 人才网职位
     *
     * @param
     * @throws Exception
     */
    @Override
    public void startNbrcJob(SpiderMonitorExt spiderMonitor) throws Exception {

        NbrcJobPageProcessor pageProcessor = new NbrcJobPageProcessor();

        NbrcJobSpider spider = new NbrcJobSpider(pageProcessor, spiderMonitor);

        Request request = new Request("https://www.nbrc.com.cn/baseApp/app/search/job?pageNumber=1&pageSize=5");
        request.setMethod(HttpConstant.Method.POST);
        request.setRequestBody(HttpRequestBody.json("{}", "utf-8"));

        spider.addRequest(request)
                //开启5个线程抓取
                .thread(1);
        spider.addPipeline(new NbrcJobPipline());

        //   SpiderMonitor.instance().register(spider);

        //添加自己的监控
        SkSpiderMonitor.instance().register(spider);
        spider.start();

    }

    /**
     * 启动一个51job的任务
     *
     * @param talentSubscriptionTaskId
     * @throws Exception
     */
    @Override
    public void startTalentJob51TaskByTalentSubscriptionTaskId(String talentSubscriptionTaskId) throws Exception {

        TalentSubscriptionTaskExt talentSubscriptionTask = talentSubscriptionTaskService.getDetail(talentSubscriptionTaskId);


        startTalentTaskByTalentSubscriptionTask(talentSubscriptionTask);


    }

    /**
     * 启动人才订阅任务
     *
     * @param talentSubscriptionTaskId
     * @throws SkException
     */
    @Override
    public void startTalentTaskByTalentSubscriptionTaskId(String talentSubscriptionTaskId) throws Exception {
        if (StringUtil.isNullOrEmpty(talentSubscriptionTaskId)) {
            return;
        }
        startTalentTaskByTalentSubscriptionTask(talentSubscriptionTaskService.getDetail(talentSubscriptionTaskId));
    }

    @Override
    public void startTalentTaskByTalentSubscriptionTask(TalentSubscriptionTaskExt talentSubscriptionTask) throws Exception {
        if (StringUtil.isNullOrEmpty(talentSubscriptionTask)) {
            return;
        }

        //说明有
        boolean job51Flag = ((NumberUtil.parseInt(talentSubscriptionTask.getChannel()) & TalentSubscriptionTaskChannelEnum.JOB51.getType().intValue()) == TalentSubscriptionTaskChannelEnum.JOB51.getType().intValue());
        if (job51Flag) {
            startJob51Spider(talentSubscriptionTask);
        }


        boolean bossFlag = ((NumberUtil.parseInt(talentSubscriptionTask.getChannel()) & TalentSubscriptionTaskChannelEnum.BOSS.getType().intValue()) == TalentSubscriptionTaskChannelEnum.BOSS.getType().intValue());
        if (bossFlag) {
            startBossSpider(talentSubscriptionTask);
        }
        boolean zhiLianFlag = ((NumberUtil.parseInt(talentSubscriptionTask.getChannel()) & TalentSubscriptionTaskChannelEnum.ZHI_LIAN.getType().intValue()) == TalentSubscriptionTaskChannelEnum.ZHI_LIAN.getType().intValue());
        if (zhiLianFlag) {
            startZhiLianSpider(talentSubscriptionTask);
        }
        boolean liePinFlag = ((NumberUtil.parseInt(talentSubscriptionTask.getChannel()) & TalentSubscriptionTaskChannelEnum.LIE_PIN.getType().intValue()) == TalentSubscriptionTaskChannelEnum.LIE_PIN.getType().intValue());
        if (liePinFlag) {
            startLiePinSpider(talentSubscriptionTask);
        }




    }

    /**
     * 启动boss 人才爬虫
     *
     * @param talentSubscriptionTask
     * @throws Exception
     */
    private void startBossSpider(TalentSubscriptionTaskExt talentSubscriptionTask) throws Exception {


    }

    /**
     * 启动智联 人才爬虫
     *
     * @param talentSubscriptionTask
     * @throws Exception
     */
    private void startZhiLianSpider(TalentSubscriptionTaskExt talentSubscriptionTask) throws Exception {


    }

    /**
     * 启动51job 人才爬虫
     *
     * @param talentSubscriptionTask
     * @throws Exception
     */
    private void startJob51Spider(TalentSubscriptionTaskExt talentSubscriptionTask) throws Exception {

        //得到token
        job51Util.initAuthorization();

        SpiderMonitorExt spiderMonitor = spiderMonitorService.createByTask(talentSubscriptionTask);

        //创建搜索条件
        TalentJob51Search job51TalentSearch = job51TalentSearchBuild.build(talentSubscriptionTask);

        //创建一个请求
        TalentJob51Request request = new TalentJob51Request();
        request.build(job51TalentSearch);
        request.addHeader("Authorization", job51Util.getAuthorization());
        request.addHeader("Cookie", job51Util.getCookie());

        TalentJob51PageProcessor pageProcessor = new TalentJob51PageProcessor();
        pageProcessor.setSearch(job51TalentSearch);
        pageProcessor.setTalentSubscriptionTaskId(talentSubscriptionTask.getId());
        pageProcessor.setJob51Util(job51Util);

        TalentJob51Pipline talentJob51Pipline = new TalentJob51Pipline();
        talentJob51Pipline.setResumeEsService(resumeEsService);
        talentJob51Pipline.setTalentSubscriptionTaskService(talentSubscriptionTaskService);


        TalentJob51Spider spider = new TalentJob51Spider(pageProcessor, spiderMonitor);
        spider.addPipeline(talentJob51Pipline);
        spider.addRequest(request);
        //添加自己的监控
        SkSpiderMonitor.instance().register(spider);
        spider.start();

    }


    @Resource
    private LiePinUtil liePinUtil;

    @Resource
    private TalentSearchLiePinBuild talentSearchLiePinBuild;
    /**
     * 启动liepin 人才爬虫
     *
     * @param talentSubscriptionTask
     * @throws Exception
     */
    private void startLiePinSpider(TalentSubscriptionTaskExt talentSubscriptionTask) throws Exception {

        //得到token
        liePinUtil.initAuthorization();

        SpiderMonitorExt spiderMonitor = spiderMonitorService.createByTask(talentSubscriptionTask);

        //创建搜索条件
        TalentLiePinSearch talentSearch = talentSearchLiePinBuild.build(talentSubscriptionTask);

        //创建一个请求
        TalentLiePinRequest request = new TalentLiePinRequest();
        request.setToken(liePinUtil.getToken());
        request.build(talentSearch);



        TalentLiePinPageProcessor pageProcessor = new TalentLiePinPageProcessor();
        pageProcessor.setSearch(talentSearch);
        pageProcessor.setTalentSubscriptionTaskId(talentSubscriptionTask.getId());
        pageProcessor.setLiePinUtil(liePinUtil);

        TalentLiePinPipline pinPipline = new TalentLiePinPipline();
        pinPipline.setResumeEsService(resumeEsService);
        pinPipline.setTalentSubscriptionTaskService(talentSubscriptionTaskService);


        TalentLiePinSpider spider = new TalentLiePinSpider(pageProcessor, spiderMonitor);
        spider.addPipeline(pinPipline);
        spider.addRequest(request);
        //添加自己的监控
        SkSpiderMonitor.instance().register(spider);
        spider.start();

    }
}
